package com.polaris.live.ui.live.widget

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.polaris.live.R
import com.polaris.live.adapter.live.TranslationTextAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.WidgetCallTranslationBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.resp.back_resp.RecognizeModel
import com.polaris.live.resp.bean.TranslationBean
import com.polaris.live.utils.image.loadAvatar
import com.polaris.live.widget.avatar.FrameImageUtils
import com.zhpan.bannerview.annotation.Visibility

/**
 * CallTranslationView
 *
 * @author Created by lucas on 2023/11/14 19:51
 */
class CallTranslationView(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    val mBinding = WidgetCallTranslationBinding.inflate(LayoutInflater.from(context), this)

    private val mContentAdapter by lazy { TranslationTextAdapter() }
    private val mTranslationContentAdapter by lazy { TranslationTextAdapter() }

    //未翻译的上一条状态         true就代表上一条已经结束了     false表示这一条还没有完事
    private var isLastType = false

    //翻译的上一条状态
    private var isTranslationLastType = false

    //上一条翻译的时间戳
    private var lastTime = 0L

    init {
        orientation = VERTICAL
    }

    @SuppressLint("ClickableViewAccessibility")
    fun initData(otherSex: Int?, avatar: String?, avatarFrame: String?, payer: Boolean) {
        if (payer) {
            mBinding.conMyContent.setBackgroundResource(R.drawable.r8_gradient_18b5fe_2a5bfc_70)
            mBinding.conOtherContent.setBackgroundResource(R.drawable.r8_gradient_back_fa3a9e_fe1849_fc772a_70)
        } else {
            mBinding.conMyContent.setBackgroundResource(R.drawable.r8_gradient_back_fa3a9e_fe1849_fc772a_70)
            mBinding.conOtherContent.setBackgroundResource(R.drawable.r8_gradient_18b5fe_2a5bfc_70)
        }

        Glide.with(mBinding.itemTranslationHead)
            .loadAvatar(avatar)
            .transform(CircleCrop())
            .into(mBinding.itemTranslationHead)
        mBinding.avFvTop.setFrameImage(FrameImageUtils.urlToFrameImageBean(avatarFrame))

        Glide.with(mBinding.itemMyTranslation)
            .loadAvatar(UserManager.getUser().avatar)
            .transform(CircleCrop())
            .into(mBinding.itemMyTranslation)
        mBinding.avFvBottom.setFrameImage(FrameImageUtils.urlToFrameImageBean(UserManager.getUser().decoration?.avatarFrame))

        mBinding.myContentRecycler.apply {
            adapter = mContentAdapter
            layoutManager = LinearLayoutManager(context).apply {
                stackFromEnd = true
            }
            itemAnimator = null
            isNestedScrollingEnabled = false
        }
        mBinding.otherContentRecycler.apply {
            adapter = mTranslationContentAdapter
            layoutManager = LinearLayoutManager(context).apply {
                stackFromEnd = true
            }
            itemAnimator = null
            isNestedScrollingEnabled = false
        }

        //拦截滑动
        mBinding.myContentRecycler.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_MOVE -> {
                    true
                }

                else -> false
            }
        }
        //拦截滑动
        mBinding.otherContentRecycler.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_MOVE -> {
                    true
                }

                else -> false
            }
        }
    }

    fun onTranslationText(otherUserBasic: UserBasicVo?, recognizeModel: RecognizeModel) {

        if (lastTime != 0L && recognizeModel.timestamp < lastTime) {
            return
        }

        lastTime = recognizeModel.timestamp

        if (!TextUtils.isEmpty(recognizeModel.content)) {
            val data = UserManager.getUser().avatar?.let {
                TranslationBean(recognizeModel.content, it, recognizeModel.timestamp)
            }

            if (data != null) {
                if (mContentAdapter.data.size == 0 || isLastType) {
                    mContentAdapter.addData(0, data)
                } else {
                    val index = mContentAdapter.data.size - 1
                    mContentAdapter.data[index] = data
                    mContentAdapter.notifyItemChanged(index)
                }
                isLastType = recognizeModel.contentType
                mBinding.myContentRecycler.scrollToPosition(mContentAdapter.data.size - 1)

                mBinding.myContentRecycler.visibility = View.VISIBLE
                if (mContentAdapter.data.size >= 10) {
                    for (i in 0 until 2) {
                        mContentAdapter.data.removeAt(i)
                    }
                    mContentAdapter.notifyItemRangeRemoved(0, 2)
                }
            }
        }

        if (!TextUtils.isEmpty(recognizeModel.translationContent)) {
            val data = otherUserBasic?.avatar?.let {
                TranslationBean(recognizeModel.translationContent, it, recognizeModel.timestamp)
            }

            if (data != null) {
                if (mTranslationContentAdapter.data.size == 0 || isTranslationLastType) {
                    mTranslationContentAdapter.addData(data)
                } else {
                    val index = mTranslationContentAdapter.data.size - 1
                    mTranslationContentAdapter.data[index] = data
                    mTranslationContentAdapter.notifyItemChanged(index)
                }
                isTranslationLastType = recognizeModel.translationContentType
                mBinding.otherContentRecycler.scrollToPosition(mTranslationContentAdapter.data.size - 1)

                mBinding.otherContentRecycler.visibility = View.VISIBLE
                if (mTranslationContentAdapter.data.size >= 10) {
                    for (i in 0 until 2) {
                        mTranslationContentAdapter.data.removeAt(i)
                    }
                    mTranslationContentAdapter.notifyItemRangeRemoved(0, 2)
                }
            }
        }
    }

    fun setVisible(@Visibility visibility: Int) {
        this.visibility = visibility
        ViewUtils.setViewVisible(this, visibility)
    }
}